package middleware

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

func IncomeWhiteList(white ...string) gin.HandlerFunc {
	mapper := make(map[string]bool)
	for _, ip := range white {
		mapper[ip] = true
	}
	none := len(mapper) == 0
	return func(ctx *gin.Context) {
		fromOrigin := ctx.RemoteIP()
		if none {
			ctx.Next()
		} else if _, ok := mapper[fromOrigin]; ok {
			ctx.Next()
		} else {
			ctx.AbortWithStatus(http.StatusForbidden)
		}
	}
}
